import { Button, Tooltip } from 'antd';
import { Fragment } from 'react';

import pageStyles from '@/common/pageLayout.less';
import { getSorter, QC_SQSTATE, SHZ_SQSTATE } from '../common/Format';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal, SHTH_CHECKSTATE } from '../../Acommon';
import { SchemaManageAPI } from '@/components/SchemaComponent/SchemaManage/type';
import { PlusOutlined, EditOutlined, RollbackOutlined, CloseCircleOutlined } from '@ant-design/icons';
import { getPosIDs, getUserID, tooltipLength } from '@/utils';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';

/** 操作按钮的显示 */
const Operation = (curRes: Record<string, any>, operateFuns: Record<string, any>, record: Record<string, any>) => {
  let operationArray: any = [];
  const { state, checkState, applyerID, applyPositionID } = record;
  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible:
        ((state == QC_SQSTATE() && !checkState && record?.createPositionID == getPosIDs() && record?.createrID == getUserID()) ||
          (state == SHZ_SQSTATE() && checkState == SHTH_CHECKSTATE() && applyerID == getUserID() && applyPositionID == getPosIDs())) &&
        curRes?.['/purchaseApplyController/editApply'],
    },
    {
      icon: <CloseCircleOutlined />,
      btnName: '作废',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible:
        state == QC_SQSTATE() &&
        !checkState &&
        curRes?.['/purchaseApplyController/cancelApply'] &&
        record?.createPositionID == getPosIDs() &&
        record?.createrID == getUserID(),
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onInvalid',
    //   isVisible:
    //     curRes?.['/purchaseApplyController/invalid'] &&
    //     record?.id &&
    //     record?.applyerID == getUserID() &&
    //     record?.applyPositionID == getPosIDs() &&
    //     record?.state == SHZ_SQSTATE(),
    // },
  ];

  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * @getApplyManageTableColumns
 * @description 申请管理数据表格信息展示和操作
 */
const getApplyManageTableColumns = ({ operation }: any, curRes): Record<string, any>[] => {
  const { onView } = operation;
  return [
    {
      title: '承办部门/团队', // 原【申请部门】
      dataIndex: 'applyDepartmentName',
      key: 'applyDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.applyDepartmentName, b?.applyDepartmentName),
      width: 250,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请编号',
      dataIndex: 'applyCode',
      key: 'applyCode',
      visible: false,
      width: 200,
      sorter: (a: any, b: any) => getSorter(a?.applyCode, b?.applyCode),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '采购项目名称',
      dataIndex: 'applyTitle',
      key: 'applyTitle',
      visible: true,
      width: 220,
      sorter: (a: any, b: any) => getSorter(a?.applyTitle, b?.applyTitle),
      render: (text: string, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              if (curRes?.['/purchaseApplyController/getApplyDetails/{id}/{currentUserID}']) {
                onView(record);
              }
            }}
            style={{ width: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },

    {
      title: '预算费用（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.expectSum, b?.expectSum),
      width: 200,
      render: (text: string) => <span>{formatDecimal(text)}</span>,
    },
    {
      title: '标的来源',
      dataIndex: 'supplyWay',
      key: 'supplyWay',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.supplyWay, b?.supplyWay),
      width: 150,
      render: (text: string | number) => {
        return <div>{text == 0 ? '月度计划' : text == 1 ? '零星采购' : text == 2 ? '实验平台' : '--'}</div>;
      },
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      sorter: (a: any, b: any) => getSorter(a?.state, b?.state),
      visible: true,
      width: 140,
      render: (text: string) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '执行状态',
      dataIndex: 'checkStateName',
      key: 'checkStateName',
      sorter: (a: any, b: any) => getSorter(a?.checkState, b?.checkState),
      visible: true,
      width: 140,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    // {
    //   title: '资金来源',
    //   dataIndex: 'projectName',
    //   key: 'projectName',
    //   sorter: (a: any, b: any) => getSorter(a?.projectName, b?.projectName),
    //   visible: true,
    //   width: 180,
    //   render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    // },
    // {
    //   title: '资金来源编号',
    //   dataIndex: 'projectCode',
    //   key: 'projectCode',
    //   sorter: (a: any, b: any) => getSorter(a?.projectCode, b?.projectCode),
    //   visible: true,
    //   width: 180,
    //   render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    // },
    {
      title: '申请时间',
      dataIndex: 'createTime', //applyTime
      key: 'createTime',
      width: 150,
      visible: true,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
    },
    {
      title: '申请理由',
      dataIndex: 'applyInfo',
      key: 'applyInfo',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.applyInfo, b?.applyInfo),
      width: 180,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人',
      dataIndex: 'applyerName',
      key: 'applyerName',
      sorter: (a: any, b: any) => getSorter(a?.applyerName, b?.applyerName),
      width: 120,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: (a: any, b: any) => getSorter(a?.createTime, b?.createTime),
      width: 150,
      visible: true,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
    },
    {
      title: '创建人公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createCompanyID > b?.createCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createPositionName > b?.createPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      sorter: (a: any, b: any) => getSorter(a?.createrName, b?.createrName),
      width: 120,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      unDrag: false,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyCompanyID > b?.modifyCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyDepartmentName > b?.modifyDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyPositionName > b?.modifyPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      width: 120,
      visible: false,
      unDrag: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 150,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
    },
    {
      title: '作废人公司',
      dataIndex: 'cancelCompanyName',
      key: 'cancelCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelCompanyID > b?.cancelCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelDepartmentName > b?.cancelDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelPositionName > b?.cancelPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 120,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废类型',
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 180,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      sorter: (a: any, b: any) => getSorter(a?.cancelReason, b?.cancelReason),
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 200,
      visible: true,
      unDrag: true,
      align: 'left',
      fixed: 'right',
      render: (text: string | null, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operation, record)}</div>;
      },
    },
  ];
};

/**
 * @getApplyManageTableHeaderActions
 * @description 申请管理表格顶部自定义按钮
 * @returns SchemaManageAPI.IActionBtnList[]
 */
const getApplyManageTableHeaderActions = (operation: any, curRes): SchemaManageAPI.IActionBtnList[] => {
  const { onAdd } = operation;
  return [
    {
      title: '添加',
      visible: curRes?.['/purchaseApplyController/addApply'],
      type: 'primary',
      icon: <PlusOutlined />,
      onClick: () => {
        onAdd();
      },
    },
  ];
};
export { getApplyManageTableColumns, getApplyManageTableHeaderActions };
